﻿using Forum.Entity.Generate.MetaDataEntity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Forum.Entity.Generate
{
	internal class Program
	{
		/// <summary>
		/// 数据库名称
		/// </summary>
		private static string dbname = "forum";

		/// <summary>
		/// 文件夹名称
		/// </summary>
		private static string filesName = "/Entitys";

		//private static string filesName = "Framework";//系统表

		/// <summary>
		/// 项目名称
		/// </summary>
		private static string modelName = "Forum.Model";

		private static void Main(string[] args)
		{
			Console.WriteLine("请输入数据库类型：");

			Console.ForegroundColor = ConsoleColor.Blue;
			Console.WriteLine("1：MsSql");
			Console.ResetColor();

			Console.ForegroundColor = ConsoleColor.Yellow;
			Console.WriteLine("2：Oracle");
			Console.ResetColor();

			string sqltype = Console.ReadLine();

			switch (sqltype)
			{
				case "1":
					DoMysqlEntity();
					break;

				case "2":
					DoOracleEntity();
					break;

				default:
					break;
			}
		}

		/// <summary>
		/// 创建mysql对象
		/// /// </summary>
		private static void DoMysqlEntity()
		{
			DapperDbContext dbhelper = new DapperDbContext(
				$"Server=127.0.0.1;Database={dbname};Uid=sa;Pwd=P@ssw0rd!2@;port=5701; Allow User Variables=True;default command timeout=120;SslMode = None",
				$"Server=127.0.0.1;Database={dbname};Uid=sa;Pwd=P@ssw0rd!2@;port=5701; Allow User Variables=True;default command timeout=120;SslMode = None"
				);
			string baseFile = AppDomain.CurrentDomain.BaseDirectory + $"../../../../{modelName}/{filesName}";

			if (!Directory.Exists(baseFile))
			{
				Directory.CreateDirectory(baseFile);
			}

			string StrSql = $@"select TABLE_NAME
                                     ,TABLE_SCHEMA
                        from  information_schema.`TABLES` where  TABLE_SCHEMA = '{dbname}'";

			var obj = dbhelper.Query<SchemaEntity>(StrSql).ToList();

			if (obj?.Count > 0)
			{
				DbToEntity dbte = new DbToEntity();
				dbte.dbhelper = dbhelper;
				dbte.dbname = dbname;
				dbte.spacename = $"{modelName}.{filesName}";
				dbte.addusing.Add("System.ComponentModel.DataAnnotations.Schema");
				dbte.addusing.Add($"{modelName}.BaseEntity");
				dbte.inherit = "StringKeyEntity";
				dbte.inheritLong = "LongKeyEntity";
				dbte.inheritNoId = "DefaultBaseEntity";

				// 排除的字段
				dbte.ignoreColumn = new List<string>() { "isactive", "cuser", "uuser", "cdate", "udate", "id" };

				foreach (var item in obj)
				{
					dbte.tablename = item.TABLE_NAME;
					//var test = item.TABLE_NAME[0..3];
					//排除系统表，生成业务表
					if (item.TABLE_NAME[0..3].ToLower() == "sys")
					{
						continue;
					}

					var ss = dbte.dousing();

					WirteCs(ss, (baseFile + "/" + item.ClassName + ".cs"));
					Console.WriteLine(item.TABLE_NAME);
				}
			}
		}

		/// <summary>
		/// 执行mssql的数据类型
		/// </summary>
		private static void DoOracleEntity()
		{
            string sqlcon = $"User ID=sa;Password=P@ssw0rd;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 2333))) (CONNECT_DATA = (SERVICE_NAME = Forum)))";
            DapperOracleContext dbhelper = new DapperOracleContext(sqlcon, sqlcon);

			string baseFile = AppDomain.CurrentDomain.BaseDirectory + $"../../../../{modelName}/{filesName}";

			if (!Directory.Exists(baseFile))
			{
				Directory.CreateDirectory(baseFile);
			}

			string StrSql = $@"SELECT * FROM user_tables WHERE Table_name LIKE 'TB_%'";

			var obj = dbhelper.Query<SchemaEntity>(StrSql).ToList();

			if (obj?.Count > 0)
			{
				OracleDbToEntity dbte = new OracleDbToEntity();
				dbte.dbhelper = dbhelper;
				//dbte.dbname = dbname;
				//dbte.spacename = "Domain.Model.AutoCode";
				//dbte.addusing = "Domain.Model";
				//dbte.inherit = "EntityBase<int>";
				dbte.dbname = dbname;
				dbte.spacename = $"{modelName}.{filesName}";
				//dbte.addusing.Add("System.ComponentModel.DataAnnotations");
				//dbte.addusing.Add("System.ComponentModel.DataAnnotations.Schema");
				//dbte.addusing.Add($"{modelName}.BaseEntity");
				dbte.inherit = "StringKeyEntity";
				dbte.inheritLong = "LongKeyEntity";
				//dbte.inheritNoId = "DefaultBaseEntity";

				foreach (var item in obj)
				{
					dbte.tablename = item.TABLE_NAME;
					//var test = item.TABLE_NAME[0..3];
					//排除系统表，生成业务表
					// if (item.TABLE_NAME[0..3].ToLower() == "sys")
					// {
					//     continue;
					// }
					var ss = dbte.dousing();

					WirteCs(ss, (baseFile + "/" + item.TABLE_NAME + ".cs"));
					Console.WriteLine(item.TABLE_NAME);
				}
			}
		}

		/// <summary>
		/// 保存到本地
		/// </summary>
		/// <param name="strCode">生成的代码</param>
		/// <param name="filePath">本地路径</param>
		public static void WirteCs(string strCode, string filePath)
		{
			using (FileStream fileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
			{
				fileStream.SetLength(0);
				var byts = Encoding.UTF8.GetBytes(strCode);
				fileStream.Write(byts, 0, byts.Length);
				fileStream.Close();
			}
		}
	}
}